package me.winds.test;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import me.winds.test.scheduler.Task;
import me.winds.test.scheduler.TaskScheduler;
import me.winds.wrapper.ViewHelper;
import me.winds.wrapper.WrapperDialog;

public class MainActivity extends AppCompatActivity {

    private static final String LAYOUT_LINEARLAYOUT = "LinearLayout";
    private static final String LAYOUT_FRAMELAYOUT = "FrameLayout";
    private static final String LAYOUT_RELATIVELAYOUT = "RelativeLayout";

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View view = null;
        if (name.equals(LAYOUT_FRAMELAYOUT)) {
            view = new AutoFrameLayout(context, attrs);
        } else if (name.equals(LAYOUT_LINEARLAYOUT)) {
            view = new AutoLinearLayout(context, attrs);
        } else if (name.equals(LAYOUT_RELATIVELAYOUT)) {
            view = new AutoRelativeLayout(context, attrs);
        }
        if (view != null) {
            return view;
        }
        return super.onCreateView(name, context, attrs);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        findViewById(R.id.btn_normal_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog();
            }
        });

        findViewById(R.id.btn_round_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TipsDialog(MainActivity.this)
                        .setUp(new TipsDialog.OnConfirmListener() {
                                   @Override
                                   public void onConfirm(Dialog dialog, View view) {
                                       showToast("你点了确定");
                                       dialog.dismiss();
                                   }
                               }, "这是处理了圆角的弹窗", "点我确定", "点我关闭"

                        ).show();
            }
        });

        findViewById(R.id.btn_custome_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomeDialog();
            }
        });

        findViewById(R.id.btn_loading_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new LoadingDailog(MainActivity.this).show();
            }
        });

        findViewById(R.id.btn_date_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });

        findViewById(R.id.btn_area_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAreaSelector();
            }
        });

    }

    private void showNormalDialog() {
        new WrapperDialog(this) {
            @Override
            public int getLayoutRes() {
                return R.layout.dialog_normal;
            }

            @Override
            protected void setDialogParams(Dialog dialog) {
                setDialogParams(dialog, Gravity.CENTER);
            }

            @Override
            public void help(ViewHelper helper) {
                helper.setOnClickListener(R.id.tv_confirm, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
            }
        }.show();
    }

    private void showCustomeDialog() {
        new CustomeDialog(this)
                .setUp(new CustomeDialog.OnConfirmListener() {
                           @Override
                           public void onConfirm(Dialog dialog, View view) {
                               dialog.dismiss();
                           }
                       }, "是否交卷",
                        "交卷后考试通过，不得重考，分数将会记录下来",
                        "交卷", "检查").show();
    }

    private void showDatePicker() {
        new DateSelector(this).addHelperAbsCallback(new WrapperDialog.HelperAbsCallback() {
            @Override
            public void help(final WrapperDialog wrapper, final Dialog dialog, ViewHelper helper) {
                helper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case R.id.tv_confirm:
                                showToast(((DateSelector) wrapper).getSelectedDate());
                                break;
                        }
                        dialog.dismiss();
                    }
                }, R.id.tv_cancel, R.id.tv_confirm);
            }
        }).show();
    }

    private void showAreaSelector() {
        showLoading();
        TaskScheduler.execute(new Task<List<CityVo>>() {
            @Override
            public List<CityVo> doInBackground() throws InterruptedException {
                String json = getFileStringFromAssets(MainActivity.this, "citys.json");
                if (json != null) {
                    return JSON.parseArray(json, CityVo.class);
                }
                return null;
            }

            @Override
            public void onSuccess(List<CityVo> result) {
                dismissLoading();
                new AreaSelector(MainActivity.this, result)
                        .addHelperAbsCallback(new WrapperDialog.HelperAbsCallback() {
                            @Override
                            public void help(final WrapperDialog wrapper, final Dialog dialog, ViewHelper helper) {
                                helper.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        switch (view.getId()) {
                                            case R.id.tv_cancel:

                                                break;
                                            case R.id.tv_confirm:
                                                String selected = ((AreaSelector) wrapper).getAreaSelected();
                                                showToast("选中的" + selected);
                                                break;
                                        }
                                        dialog.dismiss();
                                    }
                                }, R.id.tv_cancel, R.id.tv_confirm);
                            }
                        })
                        .show();
            }
        });

    }

    LoadingDailog loadingDailog;

    private void showLoading() {
        if (loadingDailog == null) {
            loadingDailog = new LoadingDailog(this);
        }
        loadingDailog.show();
    }

    private void dismissLoading() {
        if (loadingDailog != null) {
            loadingDailog.dismiss();
        }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public String getFileStringFromAssets(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStream is = context.getAssets().open(fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }
}
